
import AES

from base64 import b64encode
import requests,json

url = "https://music.163.com/weapi/comment/resource/comments/get?csrf_token="
f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
g = "0CoJUm6Qyw8W8jud"
e = "010001"
i= "1hmkfQkNHit7gIpB"

#请求方式post
# 获取真实参数
data = {
    "csrf_token": "",
    "cursor": "-1",
    "offset": "0",
    "orderType": "1",
    "pageNo": "1",
    "pageSize": "20",
    "rid": "R_SO_4_1325905146",
    "threadId": "R_SO_4_1325905146"
}

def get_encSecKey():
    return "a88c401a5103b3b7860cf64e01896228b2c6d538d741eefe8bb47bc37d698c34016a360f86519f31912f6b5662318acd10e67405071f8905dad8c8de7eaf71c09304fc3c2072743bc295086476ed1372583b4e50408d5b548ca04e4618c25cdd2733900cd6674f9e3a93330196f90e8c137325edf58bebd36facd446ae06c9d8"

def get_params(data): # 默认收到的是字符串
    first = enc_params(data,g)
    second = enc_params(first,i)
    return second

def to_16(data):
    pad = 16 - len(data) % 16
    data += chr(pad) * pad
    return data

def enc_params(data,key): #加密过程
    iv = "0102030405060708"
    data = to_16(data)
    aes = AES.new(key=key.encode("utf-8"),IV=iv.encode("utf-8"),mode=AES.MODE_CBC)
    bs = aes.encrypt(data.encode("utf-8")) # 加密，加密的内容的长度必须是16的倍数
    return str(b64encode(bs), "utf-8") #转化成字符串返回


resp = requests.post(url, data={
    "params": get_params(json.dumps(data)),  #json把字典转化成字符串
    "encSecKey": get_encSecKey()
})

content = resp.text
dict = json.loads(content)

#dict类型数据去掉dict_values()和dict_keys()前缀
# 第二个值是我们要的数据，但是一个字典
b = list(dict.values())
a = b[1]
list1_dic = a['comments']
#遍历user 获取每个user的数据
for j in list1_dic:
    nickname = j['user']['nickname']
    content = j['content']
    print("user:", nickname)
    print("content:", content)
    print("-----------------------------------------------------------------")
